package com.huowei.htzy.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.huowei.htzy.R;
import com.huowei.htzy.adapter.ClassAdapter;
import com.huowei.htzy.util.RequestInterface;
import com.huowei.htzy.util.RequestUtil;
import com.huowei.htzy.view.MyListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static com.huowei.htzy.common.Constants.URL_GOODSCLASS;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassFragment extends Fragment implements RequestInterface {


    @InjectView(R.id.class_list)
    MyListView classList;
    @InjectView(R.id.class_scrollview)
    PullToRefreshScrollView classScrollview;
    private List<Drugs.ClassListBean> listBeen = new ArrayList<>();
    private ClassAdapter classAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_class, container, false);
        ButterKnife.inject(this, view);
        //请求数据
        requestData();
        //初始化适配器
        initAdapter();
        return view;
    }

    private void initAdapter() {
        //设置到listView
        if (classAdapter == null) {
            classAdapter = new ClassAdapter(listBeen, getActivity());
        }
        classList.setAdapter(classAdapter);
        classList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    private void requestData() {
        RequestUtil util = new RequestUtil(getActivity(), this);
        util.getRequest(URL_GOODSCLASS);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onSuccess(String jsonStr) {
        //解析数据
        parseJson(jsonStr);
    }

    private void parseJson(String jsonStr) {
        listBeen.clear();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            int code = jsonObject.getInt("code");
            if (code == 200){
                JSONObject datas = jsonObject.getJSONObject("datas");
                Gson gson = new Gson();
                Drugs drugs = gson.fromJson(datas.toString(), Drugs.class);
                listBeen.addAll(drugs.getClass_list());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        classAdapter.notifyDataSetChanged();
    }

    @Override
    public void onError(VolleyError error) {
        Toast.makeText(getActivity(), "请求失败！", Toast.LENGTH_SHORT).show();
    }
}
